package com.systop.asset.controller;

import com.systop.asset.entity.Material;
import com.systop.asset.entity.RestResponse;
import com.systop.asset.entity.vo.MaterialListVo;
import com.systop.asset.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    IMaterialService materialService;

    @PostMapping("add")
    public RestResponse<?> materialAdd(@RequestBody Material material) {
        int result = materialService.addMaterial(material);
        if (result > 0) {
            return RestResponse.success();
        } else {
            return RestResponse.failure(500, "添加失败");
        }
    }

    @DeleteMapping("/del/{id}")
    public RestResponse<?> materialDel(@PathVariable Integer id) {
        int result = materialService.delMaterial(id);
        if (result > 0) {
            return RestResponse.success();
        }
        return RestResponse.failure(500, "删除失败");
    }

    @PutMapping("/editor")
    public RestResponse<?> materialUpdate(@RequestBody Material material) {
        int result = materialService.editorMaterial(material);
        if (result > 0) {
            return RestResponse.success();
        }
        return RestResponse.failure(500, "更新失败");
    }

    @GetMapping()
    public RestResponse<?> materialList(MaterialListVo vo) {
        List<MaterialListVo> materialListVos = materialService.materialList(vo);
        if (materialListVos.size() == 0) {
            vo.setTotal(0);
            materialListVos.add(vo);
        }
        return RestResponse.success(materialListVos);
    }

    /**
     * 获取耗材详情
     * @param id 耗材id
     * @param wid 仓库id
     * @return
     */
    @GetMapping("/get/{id}/{wid}")
    public RestResponse<?> getMaterial(@PathVariable("id") Long id,@PathVariable("wid") Long wid) {
        //判断id是否为空
        if (id == null) {
            return RestResponse.failure(500, "id不能为空");
        }
        //根据id查询
        Material material = materialService.getMaterialById(id,wid);
        return RestResponse.success(material);
    }

    /**
     * 快速入库
     *
     * @param material
     * @return
     */
    @PutMapping("/quickWarehouse")
    public RestResponse<?> quickWarehouseInbound(@RequestBody Material material) {
        int result = materialService.quickWarehouseInbound(material);
        if (result > 0) {
            return RestResponse.success();
        }
        return RestResponse.failure(500, "快速入库失败");
    }

    /*
     * 小程序查找耗材领用
     * */
}
